package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tongdun.TongdunWuLiuHuaXiangRecord;
import com.ucredit.babel.common.model.tongdun.TongdunWuLiuHuaXiangData;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 同盾-物流信息接口
 *
 * @author guoqiang
 */
public class TongdunWuLiuHuaXiangParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 同盾-物流信息接口
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TongdunWuLiuHuaXiangRecord obj = (TongdunWuLiuHuaXiangRecord) model;
        TongdunWuLiuHuaXiangData scoreData = obj.getData();
        JSONObject dataJson = JSON.parseObject(scoreData.getData());
        JSONObject ILogisticsProfileBasicFileV2 = dataJson.getJSONObject("result_desc").getJSONObject("ANTIFRAUD_INFOQUERY").getJSONObject("ILogisticsProfileBasicFileV2");
        JSONObject geren_xwbasearchives = ILogisticsProfileBasicFileV2.getJSONObject("geren_xwbasearchives");
        StringBuilder sb = getBaseInfo(obj, successful);
        //手机号
        sb.append(scoreData.getMobile()).append(CRT001);
        //bizCode
        sb.append(scoreData.getBizCode()).append(CRT001);
        //是否调用成功
        sb.append(dataJson.getString("success")).append(CRT001);
        //进件ID
        sb.append(dataJson.getString("id")).append(CRT001);
        //错误码
        sb.append(dataJson.getString("reason_code")).append(CRT001);
        //错误详情描述
        sb.append(dataJson.getString("reason_desc")).append(CRT001);
        //下一步
        sb.append(dataJson.getString("nextService")).append(CRT001);
        //查询结果
        sb.append(formatValue(ILogisticsProfileBasicFileV2.getString("geren_result"))).append(CRT001);
        //所属省份
        sb.append(formatValue(geren_xwbasearchives.getString("geren_province"))).append(CRT001);
        //所属城市
        sb.append(formatValue(geren_xwbasearchives.getString("geren_city_name"))).append(CRT001);
        //入网年份
        sb.append(formatValue(geren_xwbasearchives.getString("geren_register_time"))).append(CRT001);
        //黑名单状态
        sb.append(formatValue(geren_xwbasearchives.getString("geren_blacklist_flag"))).append(CRT001);
        //风险标签
        sb.append(formatValue(geren_xwbasearchives.getString("geren_risk_type"))).append(CRT001);
        sb.append(formatValue(geren_xwbasearchives.getString("geren_active_ratio"))).append(CRT001);
        sb.append(formatValue(geren_xwbasearchives.getString("geren_address_ratio"))).append(CRT001);
        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tongdunwuliu_huaxiang_inc";
    }

    @Override
    public String getType() {
        return RecordType.TONGDUN_WULIUHUAXIANG_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "同盾-物流信息接口";
    }

}
